package com.smbms.controller;

import com.mysql.cj.util.StringUtils;
import com.smbms.pojo.Bill;
import com.smbms.pojo.Provider;
import com.smbms.pojo.User;
import com.smbms.service.BillService;
import com.smbms.service.ProviderService;
import com.smbms.tools.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/bill")
public class BillController {
    @Resource
    private BillService billService;

    @Resource
    private ProviderService providerService;
    User loginUser = null;

    @GetMapping(value = "/list.html")
    public String billList(@RequestParam(required = false) String queryProductName,
                           @RequestParam(required = false ) Integer queryIsPayment,
                           @RequestParam(required = false) Integer queryProviderId, Model model) {

        Bill bill = new Bill();
        bill.setProductName(queryProductName);
        bill.setIsPayment(queryIsPayment);
        bill.setProviderId(queryProviderId);

        List<Bill> bList = billService.getBillList(bill);
        model.addAttribute("billList", bList);

        List<Provider> pList = providerService.getProvider();

        model.addAttribute("providerList", pList);
        model.addAttribute("queryProductName", queryProductName);
        model.addAttribute("queryIsPayment", queryIsPayment);
        model.addAttribute("queryProviderId", queryProviderId);
        return "billlist";
    }

    @GetMapping(value = "/add.html")
    public String toAdd() {

        return "billadd";
    }

    @RequestMapping(value = "/addsave.html", method = RequestMethod.POST)
    public String addBillSave(@Valid Bill bill, BindingResult bindingResult, HttpServletRequest request) {

        if (bindingResult.hasErrors()) {
            return "billadd";
        }

        loginUser = (User) request.getSession().getAttribute(Constants.USER_SESSION);

        bill.setCreatedBy(loginUser.getId());
        bill.setCreationDate(new Date());

        boolean result = billService.add(bill);

        if (result) {
            return "redirect:/bill/list.html";
        }
        return "billadd";
    }

    @RequestMapping(value = "/bill.do", method = RequestMethod.GET)
    @ResponseBody
    public Object providerList() {
        List<Provider> providerList = providerService.getProvider();
        return providerList;
    }

    @GetMapping(value = "/view")
    public String getList(@RequestParam String billid, Model model) {
        Bill bill = billService.getBillById(billid);
        model.addAttribute(bill);
        return "billview";
    }

    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public String modify(@RequestParam String billid, Model model) {
        Bill bill = billService.getBillById(billid);
        model.addAttribute(bill);
        return "billmodify";
    }

    @RequestMapping(value = "/billmodifysave.html", method = RequestMethod.POST)
    public String billsave(Bill bill, HttpServletRequest request) {
        loginUser = (User) request.getSession().getAttribute(Constants.USER_SESSION);
        bill.setModifyBy(loginUser.getId());
        bill.setModifyDate(new Date());
        if (billService.modify(bill)) {
            return "redirect:/bill/list.html";
        }
        return "billmodify";
    }

    @RequestMapping(value = "/delbill", method = RequestMethod.GET)
    @ResponseBody
    public Object delBill(@RequestParam String delId) {
        HashMap<String, Object> resultMap = new HashMap<>();
        if (StringUtils.isNullOrEmpty(delId)) {
            resultMap.put("delResult", "notexist");
        } else {
            boolean result = billService.deleteBillById(delId);
            if (result) {
                resultMap.put("delResult", result);
            } else {
                resultMap.put("delResult", result);
            }
        }
        return resultMap;
    }
}
